<template>
    <div>
      <el-form ref="addForm" :rules="rules" :model="form">
        <el-form-item :label="$t('message.global.group')" prop="group">
          <el-input v-model="form.group" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item :label="$t('message.global.unit')" prop="unit">
          <el-input v-model="form.unit" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item :label="$t('message.global.description')" prop="description">
          <el-input v-model="form.description" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
    </div>
</template>

<script>
    export default {
        name: "add-unit-form",
        data : function(){
          return {
            form : {
              group : '',
              unit : '',
              description : ''
            },
            rules : {
              group : [
                {required : true,message : this.$t('message.config.addForm.tips.groupNotNull'),trigger : 'blur'},
              ],
              unit : [
                {required : true,message : this.$t('message.config.addForm.tips.unitNotNull'),trigger : 'blur'},
              ],
              description : [
                {required : true,message : this.$t('message.config.addForm.tips.descriptionNotNull'),trigger : 'blur'},
              ]
            }
          }
        },
        methods : {
          groupRequired(){
            return this.$t('message.config.addForm.tips.groupNotNull');
          },
          unitRequired(){
            return this.$t('message.config.addForm.tips.unitNotNull');
          },
          descriptionRequired(){
            return this.$t('message.config.addForm.tips.descriptionNotNull');
          },

          commit(){
            this.$refs['addForm'].validate(valid => {
              if(valid){
                this.$post('/config/addUnit',this.form).then((code) => {
                  this.$emit("commitOver");
                  if(0 == code){
                    this.$message.success(this.$t('message.config.saveSuccess'));
                    this.$emit("commitSuccess");
                  }else if(1 == code){
                    this.$message.warning(this.$t('message.config.unitExist'));
                  }
                },() => {
                  this.$emit("commitOver");
                });
              }else{
                this.$emit("commitOver");
              }
            });
          }
        }
    }
</script>

<style scoped>

</style>
